package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.service.ICommonService;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.entity.OperrateLogs;
import com.boss.salary.service.IFinancialAuditService;
import com.boss.salary.service.IOperrateLogsService;
import com.boss.salary.vo.FinancialAuditVO;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <br>Title: 财政审核类
 * <br>Description:财政审核类
 * <br>Author:
 * <br>Date:
 */
@RestController
@RequestMapping(value = "/financialAuditController", produces = MediaType.APPLICATION_JSON)
public class FinancialAuditController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(FinancialAuditController.class);

    @Autowired
    private IFinancialAuditService iFinancialAuditService;

    @Autowired
    private ICommonService iCommonService;

    @Autowired
    private IOperrateLogsService iOperrateLogsService;


    @GetMapping("/queryFinancialAuditPage")
    public ApiResultDTO queryFinancialAuditPage(@RequestHeader("tokenid") String tokenid,
                                                @RequestParam("menuid") String menuid,
                                                @RequestParam("page") String page,
                                                @RequestParam("limit") String limit,
                                                @RequestParam("flag") String flag,
                                                @RequestParam("agencyId") String agencyId,
                                                @RequestParam(value = "changeAgency",required = false) String changeAgency,
                                                @RequestParam(value = "salaryType",required = false) String salaryType) {
        try {
            UserDTO userDto = getUser(tokenid);
            List<String> childrenId = iCommonService.getAgencyChildrenId(userDto, agencyId);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            Map<String, Object> queryParam = new HashMap<>(3);
            queryParam.put("flag", flag);
            queryParam.put("changeAgency", changeAgency);
            queryParam.put("salaryType", salaryType);
            queryParam.put("menuid", menuid);
            queryParam.put("province", userDto.getProvince());
            PageDTO<FinancialAuditVO> pageDTO = iFinancialAuditService.queryFinancialAuditPage(userDto, paginationDTO, queryParam, childrenId);
            return ApiResultDTO.success(pageDTO,null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败");
        }
    }


    /**
     * 财政审核，-审核通过
     * @param financialAuditVO
     * @return
     */
    @PostMapping("/financialAuditPass")
    public ApiResultDTO financialAuditPass(@RequestHeader("tokenid") String tokenid,
                                           @RequestBody FinancialAuditVO financialAuditVO) {
        try {
            UserDTO userDto = getUser(tokenid);
            //添加操作日志
            OperrateLogs operrateLogs= new OperrateLogs();
            operrateLogs.setMofDivCode(userDto.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDto.getAgency());
            operrateLogs.setUeserName(userDto.getName());
            operrateLogs.setAgencyId(String.join(",",financialAuditVO.getAgencyIds()));
            operrateLogs.setOperateName("财政审核--审核");

            iOperrateLogsService.saveOperrateLogs(operrateLogs);

            iFinancialAuditService.financialAuditPass(userDto, financialAuditVO);
            return ApiResultDTO.success(null,"审核通过成功");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "操作失败");
        }
    }

    /**
     * 财政审核，-退回
     * @param financialAuditVO
     * @return
     */
    @PostMapping("/financialAuditReject")
    public ApiResultDTO financialAuditReject(@RequestHeader("tokenid") String tokenid,
                                             @RequestBody FinancialAuditVO financialAuditVO) {
        try {
            UserDTO userDto = getUser(tokenid);
            //添加操作日志
            OperrateLogs operrateLogs= new OperrateLogs();
            operrateLogs.setMofDivCode(userDto.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDto.getAgency());
            operrateLogs.setUeserName(userDto.getName());
            operrateLogs.setAgencyId(String.join(",",financialAuditVO.getAgencyIds()));
            operrateLogs.setOperateName("财政审核--退回");
            iOperrateLogsService.saveOperrateLogs(operrateLogs);

            iFinancialAuditService.financialAuditReject(userDto, financialAuditVO);
            return ApiResultDTO.success(null,"退回成功");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "操作失败");
        }
    }

    /**
     * 财政审核，-撤销审核
     * @param financialAuditVO
     * @return
     */
    @PostMapping("/financialAuditRevoke")
    public ApiResultDTO financialAuditRevoke(@RequestHeader("tokenid") String tokenid,
                                             @RequestBody FinancialAuditVO financialAuditVO) {
        try {
            UserDTO userDto = getUser(tokenid);
            //添加操作日志
            OperrateLogs operrateLogs= new OperrateLogs();
            operrateLogs.setMofDivCode(userDto.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDto.getAgency());
            operrateLogs.setUeserName(userDto.getName());
            operrateLogs.setAgencyId(String.join(",",financialAuditVO.getAgencyIds()));
            operrateLogs.setOperateName("财政审核--撤销审核");
            iOperrateLogsService.saveOperrateLogs(operrateLogs);

            iFinancialAuditService.financialAuditRevoke(userDto, financialAuditVO);
            return ApiResultDTO.success(null,"撤销审核成功");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "操作失败");
        }
    }
}
